package com.tjbklx1.redis.chap03;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.Jedis;

import com.tjbklx1.redis.JedisUtil;

/**
 * 第三章的 基本命令
 * @author tjbklx1
 * @date 2018-12-1
 */
public class App01 {

	/**
	 * incr ,decr,incrby decrby ,// incrbyfloat
	 */
	@Test
	public void incr() {

		Jedis redis = JedisUtil.getJedis();
		String key = "number_key";
		redis.del(key);

		System.out.println(redis.get(key)); // 没有初始化,返回的是NULL

		Long temp = redis.incr(key);
		System.out.println(key + "  incr() =" + temp + "\t\t" + redis.get(key));
		temp = redis.incrBy(key, 15L);
		System.out.println(key + "  incrBy(15) =" + temp + "\t\t" + redis.get(key));
		temp = redis.decr(key);
		System.out.println(key + "  decr() =" + temp + "\t\t" + redis.get(key));
		temp = redis.decrBy(key, 15L);
		System.out.println(key + "  decrBy(15) =" + temp + "\t\t" + redis.get(key));

		System.out.println("===============================");
		String t = redis.set(key, "200"); // 返回的是OK
		System.out.println("new key =" + t + "\t\t" + redis.get(key));
		temp = redis.incr(key);
		System.out.println(key + "  incr() =" + temp + "\t\t" + redis.get(key));
	}

	/**
	 * String: append,substr,setrange
	 */
	@Test
	public void string() {
		Jedis redis = JedisUtil.getJedis();
		String key = "bit_key";
		redis.del(key);

		Long temp = redis.append(key, "hello ");// 返回字符串的总长度
		System.out.println("append 'hello ' = " + temp);
		temp = redis.append(key, "world!");
		System.out.println("append 'world!' = " + temp);

		String t = redis.substr(key, 3, 7);
		System.out.println("substr(key, 3, 7) =" + t);

		Long p = redis.setrange(key, 0, "H"); // 返回字符串的总长度
		System.out.println(p + "===" + redis.get(key));

		p = redis.setrange(key, 6, "W"); // 返回字符串的总长度
		System.out.println(p + "===" + redis.get(key));

		p = redis.setrange(key, 11, ",how are you?"); // 返回字符串的总长度
		System.out.println(p + "===" + redis.get(key));
	}

	/**
	 * bit setbit,bitcount 重点研究一下BIT的操作
	 */
	@Test
	public void bit() {
		Jedis redis = JedisUtil.getJedis();
		String key = "bit_key";
		redis.del(key);

		Boolean t = redis.setbit(key, 2, true);
		System.out.println(t + " <" + redis.get(key) + ">");

		t = redis.setbit(key, 7, true);
		System.out.println(t + " <" + redis.get(key) + ">");

		t = redis.setbit(key, 24, true);
		System.out.println(t + " <" + redis.get(key) + ">");

		System.out.println("bitcount(key) =" + redis.bitcount(key));
	}

	/**
	 * list : lpush ,lpop,rpush,rpop,lindex,lrange,ltrim<br>
	 * lindex 返回坐标的字符<br>
	 * lrange 返回子串<br>
	 * ltrim 裁剪<br>
	 */
	@Test
	public void list() {
		Jedis redis = JedisUtil.getJedis();
		String key = "list_key";
		redis.del(key);

		Long num = redis.rpush(key, "last");
		System.out.println("num = " + num);
		num = redis.lpush(key, "first");
		System.out.println("num = " + num);
		num = redis.rpush(key, "new_last");
		System.out.println("num = " + num);
		printList(redis, key);

		System.out.println("lindex(key, 2) = "+redis.lindex(key, 2));
		
		String t = redis.lpop(key);
		System.out.println(t); // pop的元素
		t = redis.lpop(key);
		System.out.println(t);
		t = redis.lpop(key);
		System.out.println(t);
//		t = redis.lpop(key);
//		System.out.println(t); //无元素,返回NULL
//		printList(redis, key);

		num = redis.rpush(key, "a", "b", "c");
		System.out.println("num = " + num);
		printList(redis, key);

		String t2 = redis.ltrim(key, 2, -1);
		System.out.println(t2); // ok
		printList(redis, key);

		t2 = redis.ltrim(key, 0, -1);
		System.out.println(t2); // ok
		printList(redis, key);
	}

	public static void printList(Jedis redis, String key) {
		List<String> resultList = redis.lrange(key, 0, -1); // 返回值是List
		if (resultList != null && resultList.size() > 0) {
			System.out.println("############################");
			for (int i = 0; i < resultList.size(); i++) {
				System.out.println(i + " : " + resultList.get(i));
			}
			System.out.println("############################");
		}
	}
	
	@Test
	public void list2() {
		Jedis redis = JedisUtil.getJedis();
		String key1 = "list_key1";
		String key2 = "list_key2";
		redis.del(key1);
		redis.del(key2);
		
		redis.rpush(key1, "item1","item2");
		redis.rpush(key2, "item3");
		redis.brpoplpush(key2, key1, 1);
		printList(redis, key1);
		printList(redis, key2);
		redis.brpoplpush(key1, key2, 1);
		printList(redis, key1);
		printList(redis, key2);
		System.out.println("====================");
		redis.blpop(1, key1,key2);
		printList(redis, key1);
		printList(redis, key2);
		redis.blpop(1, key1,key2);
		printList(redis, key1);
		printList(redis, key2);
		redis.blpop(1, key1,key2);
		printList(redis, key1);
		printList(redis, key2);
	}
	
	/**
	 * set : sadd,srem,scard,smembers,smove
	 */
	@Test
	public void set() {
		Jedis redis = JedisUtil.getJedis();
		String key1 = "set_key1";
		String key2 = "set_key2";
		redis.del(key1);
		redis.del(key2);
	
		System.out.println("sadd "+redis.sadd(key1, "a","b","c"));
		System.out.println("srem "+redis.srem(key1, "c","d"));
		System.out.println("srem "+redis.srem(key1, "c","d"));
		System.out.println("scard "+redis.scard(key1));
		System.out.println("smembers "+redis.smembers(key1));
		
		System.out.println("smove "+redis.smove(key1, key2, "a"));
		System.out.println("smove "+redis.smove(key1, key2, "c"));
		System.out.println("smembers "+redis.smembers(key1));
		System.out.println("smembers "+redis.smembers(key2));
	}
	
	/**
	 * set : sdiff,sinter,sunion,sinterstore,sunionstore
	 */
	@Test
	public void set2() {
		Jedis redis = JedisUtil.getJedis();
		String key1 = "set_key1";
		String key2 = "set_key2";
		redis.del(key1);
		redis.del(key2);
		
		redis.sadd(key1, "a","b","c","d");
		redis.sadd(key2, "c","d","e","f");
		System.out.println("sdiff "+redis.sdiff(key1,key2));
		System.out.println("sinter "+redis.sinter(key1,key2));
		System.out.println("sunion "+redis.sunion(key1,key2));
		
		//????? 不懂
		System.out.println("sdiffstore "+redis.sdiffstore(key1,key2));
		System.out.println("sinterstore "+redis.sinterstore(key1,key2));
		System.out.println("sunionstore "+redis.sunionstore(key1,key2));
		
		System.out.println("smembers "+redis.smembers(key1));
		System.out.println("smembers "+redis.smembers(key2));
	}
	
	/**
	 * hash hmset,hmget,hlen,hdel,hgetAll
	 */
	@Test
	public void hash() {
		Jedis redis = JedisUtil.getJedis();
		String key = "hash_key";
		redis.del(key);
		Map<String, String> hash = new HashMap<String,String>();
		hash.put("k1", "v1");
		hash.put("k2", "v2");
		hash.put("k3", "v3");
		hash.put("k4", "v4");
		hash.put("k5", "v5");
		System.out.println("hmset "+redis.hmset(key, hash));
		
		System.out.println("hmget "+redis.hmget(key, new String[]{"k2","k3"}));
		
		System.out.println("hlen "+redis.hlen(key));
		
		System.out.println("hdel "+redis.hdel(key, new String[]{"k2","k3"}));
		
		System.out.println("hgetAll "+redis.hgetAll(key));
		
		System.out.println("======================");
		
		System.out.println("hkeys "+redis.hkeys(key));
		System.out.println("hexists "+redis.hexists(key, "num"));
		
		System.out.println("hincrby "+redis.hincrBy(key, "num",2));
		System.out.println("hexists "+redis.hexists(key, "num"));
		System.out.println("hmget "+redis.hmget(key, "num"));
	}
	
	/**
	 * zset : zadd, zcard,zincrby,zscore,zrank,zscore,
	 * zcount,zrem,zrange,zrangewithscore
	 */
	@Test
	public void zset() {
		Jedis redis = JedisUtil.getJedis();
		String key = "zset_key";
		redis.del(key);
		
		Map<String, Double> hash = new HashMap<String,Double>();
		hash.put("k1", 1d);
		hash.put("k2", 2d);
		hash.put("k3", 3d);
		hash.put("k4", 100d);
		hash.put("k5", 50d);
		redis.zadd(key, hash);
		System.out.println(redis.zcard(key));
		System.out.println(redis.zincrby(key,10,"k4"));
		System.out.println(redis.zscore(key,"k4"));
		System.out.println(redis.zrank(key,"k4"));
		System.out.println(redis.zcount(key,0,4));
		System.out.println(redis.zrem(key,"k1"));
		System.out.println(redis.zrange(key,0,-1));
		System.out.println(redis.zrangeByScore(key,0,50));
	}
	
	@Test
	public void zset2() {
		Jedis redis = JedisUtil.getJedis();
		String key1 = "zset_key1";
		String key2 = "zset_key2";
		redis.del(key1);
		redis.del(key2);
		
		Map<String, Double> hash = new HashMap<String,Double>();
		hash.put("a", 1d);
		hash.put("b", 2d);
		hash.put("c", 3d);
		
		Map<String, Double> hash2 = new HashMap<String,Double>();
		hash2.put("b", 4d);
		hash2.put("c", 1d);
		hash2.put("d", 0d);
		
		redis.zadd(key1, hash);
		redis.zadd(key2, hash2);
		
		System.out.println(redis.zinterstore("zset-i", new String[]{key1,key2}));
		printZSet(redis,"zset-i");
//		redis.zunionstore("zset-u", ZParams.Aggregate.MIN, new String[]{key1,key2});
		//???
		
		
		
	}
	
	private void printZSet(Jedis redis, String key) {
		Set<String> set = redis.zrangeByScore(key, 0, 1000); //根据有序集合的score //返回值SET
		System.out.println("###########zrangeByScore#################");
		for (String str : set) {
			System.out.println(str);
		}
		System.out.println("############################");
	}
}
